﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AES小工具
{
    public partial class FrmDateTime : Form
    {
        public FrmDateTime()
        {
            InitializeComponent();
        }

        readonly string format1s = "yyyy-MM-dd HH:mm:ss";

        readonly string format1ms = "yyyy-MM-dd HH:mm:ss.fff";

        private void btnGetCurrDateTime1s_Click(object sender, EventArgs e)
        {
            tbCurrDateTime1s.Text = DateTime.Now.ToString(format1s);
        }

        private void btnTran1s_Click(object sender, EventArgs e)
        {
            if (tbCurrDateTime1s.Text.Length > 0)
            {
                DateTime dt = Convert.ToDateTime(tbCurrDateTime1s.Text);
                DateTimeOffset dtOffset = new DateTimeOffset(dt.Year,dt.Month,dt.Day,dt.Hour,dt.Minute,dt.Second,TimeSpan.Zero);
                long timestamp = dtOffset.ToUnixTimeSeconds();
                tbTimestamp1s.Text = timestamp.ToString();
            }
            else if (tbTimestamp1s.Text.Length > 0)
            {
                long timestamp = long.Parse(tbTimestamp1s.Text);
                DateTimeOffset timeOffset = DateTimeOffset.FromUnixTimeSeconds(timestamp);
                tbCurrDateTime1s.Text += timeOffset.ToString(format1s);
            }
            else {
                MessageBox.Show("没有需要转换的数据");
            }
        }

        private void btnGetCurrDateTime1ms_Click(object sender, EventArgs e)
        {
            tbCurrDateTime1ms.Text = DateTime.Now.ToString(format1ms);
        }

        private void btnTran1ms_Click(object sender, EventArgs e)
        {
            if (tbCurrDateTime1ms.Text.Length > 0)
            {
                DateTime dt = Convert.ToDateTime(tbCurrDateTime1ms.Text);
                DateTimeOffset dtOffset = new DateTimeOffset(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second,dt.Millisecond, TimeSpan.Zero);
                long timestamp = dtOffset.ToUnixTimeMilliseconds();
                tbTimestamp1ms.Text = timestamp.ToString();
            }
            else if (tbTimestamp1ms.Text.Length > 0)
            {
                long timestamp = long.Parse(tbTimestamp1ms.Text);
                DateTimeOffset timeOffset = DateTimeOffset.FromUnixTimeMilliseconds(timestamp);
                tbCurrDateTime1ms.Text += timeOffset.ToString(format1ms);
            }
            else
            {
                MessageBox.Show("没有需要转换的数据");
            }
        }

        private void btnGetCurrDateTime2006_Click(object sender, EventArgs e)
        {
            tbCurrDateTime2006.Text = DateTime.Now.ToString(format1s);
        }

        private void btnTran2006_Click(object sender, EventArgs e)
        {
            DateTime dt2006 = Convert.ToDateTime("2006-01-01 00:00:00");
            if (tbCurrDateTime2006.Text.Length > 0)
            {
                DateTime dt = Convert.ToDateTime(tbCurrDateTime2006.Text);
                TimeSpan ts = dt - dt2006;
                long sec = (long)ts.TotalSeconds;
                tbTimestamp2006.Text = sec.ToString();
            }
            else if (tbTimestamp2006.Text.Length > 0)
            {
                long sec = long.Parse(tbTimestamp2006.Text.Trim());
                dt2006 = dt2006.AddSeconds(sec);
                tbCurrDateTime2006.Text = dt2006.ToString(format1s);
            }
            else {
                MessageBox.Show("没有需要转换的数据");
            }
        }
    }
}
